package com.example.android_app.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.android_app.MainActivity;
import com.example.android_app.database.MyDataBase;
import com.example.android_app.databinding.ActivityLoginBinding;



public class LoginActivity extends AppCompatActivity {
    ActivityLoginBinding activityLoginBinding;
    MyDataBase myDataBase;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityLoginBinding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(activityLoginBinding.getRoot());
        myDataBase = new MyDataBase(LoginActivity.this, "DB", 1);
        goToRegister();
        commit();
        resetPassword();
    }

    private void commit() {
        activityLoginBinding.login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isValid()){
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                }
            }
        });
    }

    private void goToRegister(){
        activityLoginBinding.newRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });
    }

    // 后续增加判断用户输入是否合理（这里仅作简单的判断)
    private Boolean isValid(){

        String username_value = activityLoginBinding.username.getText().toString().trim();
        String password_value = activityLoginBinding.password.getText().toString().trim();
        if (TextUtils.isEmpty(username_value)){
            Toast.makeText(LoginActivity.this, "请输入账号", Toast.LENGTH_SHORT).show();
            return false;
        }else if(TextUtils.isEmpty(password_value)) {
            Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (myDataBase.query(username_value, password_value)){
            Toast.makeText(LoginActivity.this,"登录成功", Toast.LENGTH_SHORT).show();
            return true;
        }else {
            Toast.makeText(LoginActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
            return false;
        }
    }

    private void resetPassword(){
        Button reset_password_button = activityLoginBinding.resetPassword;
        reset_password_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    final EditText reset_password = new EditText(LoginActivity.this);
                    new AlertDialog.Builder(LoginActivity.this).setTitle("请输入新的密码")
                            .setIcon(android.R.drawable.sym_def_app_icon)
                            .setView(reset_password)
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    String username_value = activityLoginBinding.username.getText().toString().trim();
                                    String reset_password_value = reset_password.getText().toString().trim();
                                    if (TextUtils.isEmpty(username_value)) {
                                        Toast.makeText(LoginActivity.this, "请先输入用户名", Toast.LENGTH_SHORT).show();
                                    }else if (!myDataBase.query_username(username_value)) {
                                        Toast.makeText(LoginActivity.this, "用户名不存在", Toast.LENGTH_SHORT).show();
                                    }else if(TextUtils.isEmpty(reset_password_value)) {
                                        Toast.makeText(LoginActivity.this, "请输入新的密码", Toast.LENGTH_SHORT).show();
                                    }else {
                                        myDataBase.updateData(username_value, reset_password_value);
                                        Toast.makeText(LoginActivity.this, "重新设置密码成功", Toast.LENGTH_SHORT).show();
                                    }



                                    //按下确定键后的事件
                                }
                            }).setNegativeButton("取消",null).show();
                }
        });
    }
}
